/* Autostop
 *
 * Copyright (c) Maciej Gajewski, Grzegorz Latarowski
 * http://code.google.com/p/autostop-automaty-drogowe
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version. NO WARRANTY OF ANY KIND is expressed or implied.
 */

/// @file registers.h routines acessing camera registers from userspace
/// using driver's ioctls

#ifndef REGISTERS_H
#define REGISTERS_H

#include <stdint.h>

// ============================================================================
/// Reads value of camera register at CS0.
/// The registers are 16-bit wide, ad addresses are actually a word numbers,
/// starting from 0.
/// @param device	open device file descriptor
/// @param address	register address.
/// @return		readed value as unsigned word in younger bytes, or -1 on error
int read_register( int device, int address );

// ============================================================================
/// Reads multiple value of camera register at CS0
///	Stores high-order bytes of each word-sized register into supplied buffer.
/// @param device	open device file descriptor
/// @param address	register address.
/// @param buffer	output data buffer
/// @param count	number of reads
/// @return 0 on success, negative value on failure
int read_multi_hibytes( int device, int address, uint8_t* buffer, size_t count );

// ============================================================================
/// Reads multiple value of camera register at CS0 into supplied buffer.
/// @param device	open device file descriptor
/// @param address	register address.
/// @param buffer	output data buffer
/// @param count	number of reads
/// @return 0 on success, negative value on failure
int read_multi_bytes( int device, int address, uint8_t* buffer, size_t count );

// ============================================================================
/// Writes value to camera register at CS0.
/// The registers are 16-bit wide, ad addresses are actually a word numbers,
/// starting from 0.
/// @param device	open device file descriptor
/// @param address	register address.
/// @param value	value to be written (unsigned word)
/// @return 0 on success, negative value on failure
int write_register( int device, int address, int value );

// ============================================================================
/// Writes value to CCD register.
///	Uses auxiliary processor and it's tex tinterface avilable by in-FPGA pipe.
/// It send s the 'wrccd /addr/ /val/\r' command via the pipe.
/// @param device	open device file descriptor
/// @param address	register address.
/// @param value	value to be written
/// @return 0 on success, negative value on failure
int write_ccd_register( int device, int address, int value );

// ============================================================================
/// Writes tect command to auciliary processor via in-FPGA pipe
/// @param device	open device file descriptor
/// @param comand	command tect
int write_command( int device, const char* command );


// registers and their bits  bits
// FIXME get info from RP complete the documentation

// 		readed registers

#define CRD_STAT			0		// state register - read
#define CSTAT_FULLXP		4		// - slave processor write pipe full
#define CSTAT_NZPX			8		// - slave procesor read pipe not empty
#define CSTAT_STJP			16		// - jpeg active
#define CSTAT_UNUSED		32		// - unused (was ffnz)
#define CSTAT_FFNZ			64		// - image read pipe not empty

#define CRD_FFP				1		// slave procesr read pipe

#define CRD_ILEJ			2		// availabel data count (in dwords)

#define CRD_FF				3		// data output

#define CRD_STAT1			4		// second state - read
#define CSTAT1_JNBJ			1		// - jpeg data avilable
#define CSTAT1_JNBY			2		// - raw data avilable
#define CSTAT1_JNBYY		4		// - background/segmented avialable
#define CSTAT1_JNBJ2		8		// - jpeg (2nd memory bank) data avilable
#define CSTAT1_JNBY2		16		// - raw data (2nd memory bank) data avilable

#define CRD_ACCEL_XR		32		// Accelerometer - X-axis reference counter
#define CRD_ACCEL_XV		33		// Accelerometer - X-axis value counter
#define CRD_ACCEL_YR		34		// Accelerometer - Y-axis reference counter
#define CRD_ACCEL_YV		35		// Accelerometer - Y-axis value counter

//		written registers

#define CWR_CR				0		// control register
#define CCR_CZYTJX			1		// - ? read jpeg
#define CCR_CZYTYX			2		// - ? read raw data
#define CCR_CZYTYRX			4		// - read backgorund image
#define CCR_CZYTRX			8		// - read segmented image
#define CCR_ENYX			16		// - ? enable acquisition of raw data
#define CCR_ENRX			32		// - ? enable acquisition ofdifferential data
#define CCR_ENJ				64		// - ? enable acquisition of jpeg

#define CWR_FF				1		// slave cpu console input

#define CWR_WINDOW			2		// okno d[12:2]; [1:0]=>xp,yp,xk,yk; x>=xp and x<=xk; y>=yp and y<=yk;!nie dziala dla ostatniej linii.

#define CWR_FGTRESHOLD		3		// d[7:0] foreground treshold (pix is fg if |val-bg| > this

#define CWR_BGFACTOR		4		// d[7:0] bgfactor - how much backgrounf is updated (in 1/256's)


#endif // REGISTERS_H


// eof

